<template>
  <div class="student-container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="
        $router.back();
        socket.disconnect();
      "
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list" ref="char">
      <template v-for="(item, index) in list">
        <!-- 左侧是机器人小智 -->
        <div class="chat-item left" :key="index" v-if="item.isStu">
          <van-image fit="cover" round :src="student" />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>
        <!-- 右侧是当前用户 -->
        <div class="chat-item right" :key="index" v-else>
          <div class="chat-pao my">{{ item.msg }}</div>
          <van-image fit="cover" round :src="teacher" />
        </div>
      </template>
    </div>
    <div class="reply-container van-hairline--top">
      <van-search placeholder="说点什么..." v-model="value" @search="onSearch">
      </van-search>
    </div>
  </div>
</template>

<script>
/* 细节： van-img不支持读取本地图片
   解决方案：使用import导入图片资源
*/
import student from "@/assets/student.jpg";
import teacher from "@/assets/teacher.png";
//导入websocket.io-client
import { io } from "socket.io-client";

export default {
  name: "student",
  data() {
    return {
      student, //学生图片
      teacher, //老师图片
      socket: null,
      list: [], //聊天内容列表
      value: "", //输入框文本
    };
  },
  created() {
    //1.连接scoket
    this.socket = io("http://118.24.62.36:8888");
    //2.连接成功回调事件
    this.socket.on("connect", () => {
      console.log("scoket连接成功");
    });
    //3.接收消息
    this.socket.on("message", (res) => {
      console.log(res);
      //绑定数据
      this.list.push({
        msg: res.msg,
        timestamp: res.timestamp,
        isStu: true,
      });
    });
  },
  methods: {
    //4.发送消息
    onSearch() {
      //(1)非空判断
      if (this.value == "") {
        this.$toast.fail("说点什么吧");
        return;
      }
      //(2)发送消息
      let time = Date.now();
      this.socket.emit("message", {
        msg: this.value,
        timestamp: time,
      });
      //(3)添加到列表
      this.list.push({
        msg: this.value,
        timestamp: time,
        isStu: false,
      });
      //(4)文本清空
      this.value = "";
      //   自动滚到最底部
      this.$nextTick(() => {
        this.$refs.char.scrollTop = this.$refs.char.offsetHeight;
      });
    },
  },
};
</script>

<style lang="less">
.student-container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
      .chat-pao.my {
        background-color: #9eea6a;
        &::before {
          content: "";
          background: #9eea6a;
        }
      }
    }
  }
  .chat-item.right {
    text-align: right;
    .chat-pao {
      margin-left: 0;
      margin-right: 15px;
      &::before {
        right: -6px;
        transform: rotate(45deg);
      }
    }
  }
  .chat-item.left {
    text-align: left;
    .chat-pao {
      margin-left: 15px;
      margin-right: 0;
      &::before {
        left: -5px;
        transform: rotate(-135deg);
      }
    }
  }
  .reply-container {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 44px;
    width: 100%;
    background: #f5f5f5;
    z-index: 9999;
  }
}
</style>